package br.com.agenda.web.client.popup;

import java.util.HashMap;
import java.util.Map;

import br.com.agenda.web.client.UsuarioService;
import br.com.agenda.web.client.UsuarioServiceAsync;
import br.com.agenda.web.client.constant.CadastroUsuarioConstants;
import br.com.agenda.web.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class CadastroUsuarioPopUp extends DialogBox {
	private CadastroUsuarioConstants	constants;
	private FlexTable layLogin;
	private TextBox txNome;
	private PasswordTextBox txSenha;
	private PasswordTextBox txConfirmaSenha;
	
	private final UsuarioServiceAsync	usuarioService	= GWT.create(UsuarioService.class);

	public CadastroUsuarioPopUp() {
		super();

		constants = GWT.create(CadastroUsuarioConstants.class);
		constructPopUp();
	}

	private void constructPopUp() {
		setText(constants.hdTitulo());

		layLogin = new FlexTable();
		layLogin.setCellSpacing(2);

		FlexCellFormatter cellLogin = layLogin.getFlexCellFormatter();
		cellLogin.setColSpan(6, 0, 2);
		cellLogin.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		cellLogin.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		cellLogin.setHorizontalAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		cellLogin.setHorizontalAlignment(6, 0, HasHorizontalAlignment.ALIGN_RIGHT);

		txNome = new TextBox();
		txNome.setFocus(true);

		layLogin.setHTML(0, 0, constants.lbNome());
		layLogin.setWidget(0, 1, txNome);

		txSenha = new PasswordTextBox();

		layLogin.setHTML(2, 0, constants.lbSenha());
		layLogin.setWidget(2, 1, txSenha);

		txConfirmaSenha = new PasswordTextBox();

		layLogin.setHTML(4, 0, constants.lbConfirmaSenha());
		layLogin.setWidget(4, 1, txConfirmaSenha);

		Button btnOk = new Button(constants.btOk());
		btnOk.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if( ! validarCampos()){
					return;
				}
				
				Map<String, Object> data = new HashMap<String, Object>();
				
				data.put("nome", txNome.getValue());
				data.put("senha", txSenha.getValue());
				
				usuarioService.cadastrar(data, new AsyncCallback<Void>() {
					public void onSuccess(Void result) {
						
					}
					
					public void onFailure(Throwable caught) {
						
					}
				});
			}
		});

		Button btnCancelar = new Button(constants.btCancelar());
		btnCancelar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide(true);
			}
		});

		HorizontalPanel hControl = new HorizontalPanel();
		hControl.setSpacing(5);
		hControl.add(btnOk);
		hControl.add(btnCancelar);

		layLogin.setWidget(6, 0, hControl);

		setWidget(layLogin);
	}
	
	private boolean validarCampos(){
		boolean anyError = false;
		
		if(txSenha.getValue().length() == 0){
			layLogin.setHTML(3, 1, constants.errorCampoRequerido());
			anyError = true;
		}
		
		if(txConfirmaSenha.getValue().length() == 0){
			layLogin.setHTML(5, 1, constants.errorCampoRequerido());
			anyError = true;
		}else if( ! txConfirmaSenha.getValue().equals(txSenha.getValue())){
			layLogin.setHTML(5, 1, constants.errorSenhaDiferente());
			anyError = true;
		}
		
		if( ! FieldVerifier.isValidName(txNome.getValue())){
			layLogin.setHTML(1, 1, constants.errorMinNome());
			anyError = true;
		}
		
		return ! anyError;
	}
}
